package com.ll.studentservice.controller;


import com.ll.commonUtils.JwtUtils;
import com.ll.commonUtils.MD5;
import com.ll.commonUtils.R;
import com.ll.studentservice.entity.EduInstructor;
import com.ll.studentservice.entity.EduTeacher;
import com.ll.studentservice.entity.vo.*;
import com.ll.studentservice.service.EduInstructorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 辅导员 前端控制器
 * </p>
 *
 * @author ll
 * @since 2020-12-07
 */
@Api(description = "辅导员登录注册信息增删改查API") //swagger界面提示
@RestController
@RequestMapping("/studentservice/eduinstructor")
public class EduInstructorController {

    @Autowired
    private EduInstructorService eduInstructorService;

    //查询单个辅导员
    @GetMapping("findOne/{id}")
    @ApiOperation(value = "查询单个辅导员(根据教师Id)") //swagger界面提示
    public R findOneStudent(@PathVariable String id){
        EduInstructor eduInstructor = eduInstructorService.getById(id);
        EduInstructorInfo eduInstructorInfo = new EduInstructorInfo();
        BeanUtils.copyProperties(eduInstructor, eduInstructorInfo);

        return R.ok().data("eduInstructor", eduInstructorInfo);
    }


    //查询所有辅导员
    @GetMapping("findAllInstructor")
    @ApiOperation(value = "查询所有辅导员") //swagger界面提示
    public R findAllInstructor(){
        List<EduInstructor> eduInstructorList = eduInstructorService.list(null);

        //过滤掉密码
       List<EduInstructorInfo> eduInstructorInfoList = new ArrayList<>();
        for (EduInstructor eduInstructor : eduInstructorList) {
            //创建没有密码属性的辅导员实体类
            EduInstructorInfo eduInstructorInfo = new EduInstructorInfo();

            //批量赋值
            BeanUtils.copyProperties(eduInstructor, eduInstructorInfo);

            //添加到集合中
            eduInstructorInfoList.add(eduInstructorInfo);
        }


        //判断集合是否为空
        if (eduInstructorInfoList.size() == 0){
            return R.error().message("记录为空!");
        } else {
            return R.ok().data("allInstructorInfo", eduInstructorInfoList);
        }
    }

    //添加辅导员
    @ApiOperation("手动添加辅导员")
    @PostMapping("addInstructor")
    public R addInstructor(@RequestBody AddInstructor addInstructor){
        //加密密码
        addInstructor.setPassword(MD5.encrypt(addInstructor.getPassword()));

        EduInstructor eduInstructor = new EduInstructor();
        BeanUtils.copyProperties(addInstructor, eduInstructor);

        boolean save = eduInstructorService.save(eduInstructor);

        // 三元运算符
        return save ? R.ok().message("添加辅导员信息成功") : R.error().message("添加辅导员信息失败");

    }

    //辅导员登录
    @ApiOperation(value = "辅导员登录,传递手机号和密码,暂时不需要验证码") //swagger界面提示
    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo loginVo){
        //loginVo对象里面封装手机号和密码
        //登录成功,返回token值,使用jwt生成
        String token = eduInstructorService.login(loginVo);

        return  R.ok().data("token", token);
    }

    //根据token获取用户信息
    @ApiOperation(value = "根据token获取用户信息(swagger里面不能测试,要在Postman里测试,参数:header中的key为token,value为登录后返回的token字符串)") //swagger界面提示
    @GetMapping("getInstructorInfo")
    public R getInstructorInfo(HttpServletRequest request){
        System.out.println("EduInstructorController ==> getInstructorInfo(根据token获取用户信息)");

        //调用jwt工具类的方法,根据request对象获取头信息,返回用户id
        String teacherId = JwtUtils.getMemberIdByJwtToken(request);

        //查询数据库,根据用户id查询用户数据
        EduInstructor eduInstructor = eduInstructorService.getById(teacherId);

        if (StringUtils.isEmpty(eduInstructor)){
            return R.error().message("用户未登陆");
        }

        EduInstructorInfo eduInstructorInfo = new EduInstructorInfo();
        BeanUtils.copyProperties(eduInstructor, eduInstructorInfo);

        return R.ok().data("eduInstructor", eduInstructorInfo);
    }

}

